<template>
  <div class="mask" @click="clickMask" v-if="showMask"></div>
  <Layout class="container" ref="container">
    <BlogList />
    <template #right>
      <BlogCategory class="right" :class="{ have: !isShow }" @select="handleSelect" />
    </template>
  </Layout>
  <Teleport to="body" disabled="true">
    <div
      ref="muluBtn"
      class="muluBtn"
      :class="{ remove: !isShow }"
      @click="handleClick"
      @touchmove="touchmove($event)"
      @touchstart="touchstart($event)"
      @touchend="touchend($event)"
    >
      分类
    </div>
  </Teleport>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import BlogList from './components/BlogList.vue'
import BlogCategory from './components/BlogCategory.vue'
const showMask = ref(false)
const isShow = ref(true)
const muluBtn = ref<any>(null)
const container = ref<any>(null)
const disX = ref(0)
const disY = ref(0)
const clickMask = () => {
  showMask.value = false
  isShow.value = true
}
const handleClick = function () {
  isShow.value = false
  showMask.value = true
}
const handleSelect = function () {
  isShow.value = true
  showMask.value = false
}
// 手指落下时触发
function touchstart(event: any) {
  const rect = muluBtn.value.getBoundingClientRect()
  disX.value = event.targetTouches[0].clientX - rect.left
  disY.value = event.targetTouches[0].clientY - rect.top
}

// 手指移动时触发
function touchmove(event: any) {
  // 2，获取手指移动的实时位置  需要减去位置差
  let moveX = event.targetTouches[0].clientX - disX.value
  let moveY = event.targetTouches[0].clientY - disY.value
  // 5，开始移动
  muluBtn.value.style.left = moveX + 'px'
  muluBtn.value.style.top = moveY + 'px'
}
function touchend(event: any) {
  if (event.changedTouches[0].clientX < container.value.$el.clientWidth / 2) {
    muluBtn.value.style.left = 5 + 'px'
  } else {
    const width = muluBtn.value.getBoundingClientRect().width
    muluBtn.value.style.left = container.value.$el.clientWidth - width - 5 + 'px'
  }
}
</script>

<style lang="scss" scoped>
.right {
  @media screen and (max-width: 992px) {
    display: none;
    position: absolute;
    right: 0;
  }
}

.mask {
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.55);
  position: absolute;
  z-index: 7;
}

.muluBtn {
  display: none;

  @media screen and (max-width: 992px) {
    display: block;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    top: 60%;
    right: 5px;
    border-radius: 50%;
    position: absolute;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.55);
  }

  &:hover {
    cursor: pointer;
  }
}

.remove {
  display: none;
}

.have {
  display: block;
  z-index: 9;
  background-color: #fff;
  width: 150px;
}
</style>
